import React, { useEffect, useState } from "react";
import { Space, Table, Card, Switch, Modal, Button, Drawer } from "antd";
import type { ColumnsType } from "antd/es/table";
import { useSelector } from "react-redux";
import { RootState } from "@/redux";
import useModal from "@/hooks/useModal";
import useMessage from "@/hooks/useMessage";
import styles from "./index.module.less";
import { getWorkDetail, getWorkList, removeWork } from "@/redux/modules/work/action";
import WorkDetail from "../WorkDetail";
import WorkAdd from "../WorkAdd";
import WorkFilter from "../WorkFilter";
import { Work } from "@/api/interface/work";

const WorkList: React.FC = props => {
	interface DataType {
		id: number;
		key: number;
		work_title: string;
		work_des: string;
		createdAt: string;
		updatedAt: string;
	}
	[];

	const columns: ColumnsType<DataType> = [
		{
			title: "标题",
			dataIndex: "work_title",
			key: "work_title",
			ellipsis: true
		},
		{
			title: "描述",
			dataIndex: "work_des",
			key: "work_des",
			ellipsis: true
		},
		{
			title: "创建日期",
			dataIndex: "createdAt",
			key: "createdAt"
		},
		{
			title: "修改日期",
			dataIndex: "updatedAt",
			key: "updatedAt"
		},
		{
			title: "操作",
			key: "action",
			render: (_, row) => {
				return (
					<>
						<Space size="middle">
							<a onClick={() => openDetail(row.id)}>详情</a>
							<a onClick={() => openEdit(row.id)}>修改</a>
							<a onClick={() => openModal(row.id)}>删除</a>
						</Space>
					</>
				);
			}
		}
	];

	const listExtra = (
		<div style={{ display: "flex", gap: 8 + "px" }}>
			<Button type="primary" onClick={() => setAddVisible(true)}>
				新增
			</Button>
			<Button type="primary" onClick={() => getWorkList()}>
				刷新
			</Button>
		</div>
	);

	const { contextHolder, success } = useMessage();
	const { id, isModalOpen, openModal, closeModal } = useModal();

	const deleteCircleFriend = () => {
		removeWork(id);
		closeModal();
		success("删除成功");
	};

	const workList = useSelector((state: RootState) => state.work.workList);

	const data: DataType[] = workList.map(item => {
		return {
			id: item.id,
			key: item.id,
			work_des: item.work_des,
			work_title: item.work_title,
			createdAt: item.createdAt,
			updatedAt: item.updatedAt
		};
	});

	useEffect(() => {
		getWorkList();
	}, []);

	// 详情
	const [detailVisible, setDetailVisible] = useState(false);
	const openDetail = (id: number) => {
		// 获取详情
		getWorkDetail(id);
		setDetailVisible(true);
	};

	// 修改
	const [editVisible, setEditVisible] = useState(false);
	const openEdit = async (id: number) => {
		getWorkDetail(id);
		setEditVisible(true);
	};
	const onCloseEdit = () => {
		getWorkList();
		setEditVisible(false);
	};

	// 新增
	const [addVisible, setAddVisible] = useState(false);
	const onCloseAdd = () => {
		getWorkList();
		setAddVisible(false);
	};

	// 搜索
	const onSearch = (data?: Work) => {
		getWorkList(1, 10, data);
	};

	return (
		<div className={styles.root}>
			{contextHolder}
			<WorkFilter onSearch={onSearch} />
			<Card title="数据列表" extra={listExtra}>
				<Table columns={columns} dataSource={data} />
			</Card>

			<Modal title="提醒" mask={false} visible={isModalOpen} onOk={deleteCircleFriend} onCancel={closeModal}>
				确定删除该作品吗
			</Modal>

			<Drawer title="详情" placement="right" width={500} onClose={() => setDetailVisible(false)} visible={detailVisible}>
				<WorkDetail></WorkDetail>
			</Drawer>

			<Drawer
				title="新增"
				placement="right"
				width={500}
				onClose={() => {
					setAddVisible(false);
				}}
				visible={addVisible}
			>
				<WorkAdd onComfirm={onCloseAdd} visible={addVisible}></WorkAdd>
			</Drawer>

			<Drawer
				title="修改"
				placement="right"
				width={500}
				onClose={() => {
					setEditVisible(false);
				}}
				visible={editVisible}
			>
				<WorkAdd type="edit" onComfirm={onCloseEdit}></WorkAdd>
			</Drawer>
		</div>
	);
};

export default WorkList;
